package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.bus.client.entity.BusOrder;
import com.meida.module.bus.client.entity.BusOrderQuestion;
import com.meida.module.bus.client.enums.UserTypeEnum;
import com.meida.module.user.client.entity.AppUser;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 后台订单追问列表
 */
@Component
public class BusOrderQuestionPageListHandler implements PageInterceptor<BusOrderQuestion> {

    @Override
    public void prepare(CriteriaQuery<BusOrderQuestion> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        cq.select(BusOrderQuestion.class, "questionId", "orderId", "userId", "userType", "content", "createTime");
        cq.select(BusOrder.class, "orderNo");
        cq.select(AppUser.class, "userName", "nickName", "mobile");
        cq.like(BusOrder.class, "orderNo");
        cq.like(AppUser.class, "userName");
        cq.like(AppUser.class, "nickName");
        cq.like(AppUser.class, "mobile");
        cq.eq(BusOrderQuestion.class, "orderId");
        cq.ge("DATE_FORMAT(orderQuestion.createTime,'%Y-%m-%d')", beginDate);
        cq.le("DATE_FORMAT(orderQuestion.createTime,'%Y-%m-%d')", endDate);
        cq.createJoin(BusOrder.class);
        cq.createJoin(AppUser.class);
    }

    @Override
    public void complete(CriteriaQuery<BusOrderQuestion> cq, List<EntityMap> result, EntityMap extra) {
        result.forEach(entityMap -> {
            String userType = entityMap.get("userType");
            UserTypeEnum userTypeEnum = UserTypeEnum.getByValue(userType);
            entityMap.put("userTypeText", "");
            if (FlymeUtils.isNotEmpty(userTypeEnum)) {
                entityMap.put("userTypeText", userTypeEnum.getLabel());
            }
        });
    }
}
